package person.drf.shop.product.web;

import org.redisson.api.RCountDownLatch;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import person.drf.shop.product.entity.CategoryEntity;
import person.drf.shop.product.service.CategoryService;
import person.drf.shop.product.vo.Catelog2Vo;
import sun.plugin.util.UIUtil;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Controller
public class IndexController {

    @Autowired
    CategoryService categoryService;

    @Autowired
    RedissonClient redissonClient;

    @Autowired
    StringRedisTemplate redisTemplate;

    // 首页  并携带一级分类数据
    @RequestMapping({"/","/index.html"})
    public String  indexPage(Model model){
        //查出所有1级分类
        List<CategoryEntity> categoryEntityList = categoryService.getLevel1CateGorys();

        model.addAttribute("categorys",categoryEntityList);

        return "index";
    }

    //获取首页三级分类数据
    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String, List<Catelog2Vo>> getCategoryJson(){

        Map<String, List<Catelog2Vo>> categoryJson = categoryService.getCategoryJson();

        return categoryJson;
    }

    //测试 可重入锁
    @ResponseBody
    @GetMapping("/hello")
    public String hello(){
        //1.获取一把锁，只要名字一样就是同一把锁
        RLock lock = redissonClient.getLock("my-lock");

        //2.加锁
        // 阻塞式等待，默认是30s
        // 1） 锁的自动续期，如果业务时间超长，运行期间自动给锁续上新的30秒，不用担心业务时间长，锁会过期自动删掉
        // 2） 加锁业务只要完成，就不会当前锁续期，既使不手动解锁，默认在30秒以后解锁
        // 这就是redisson的看门狗原理
        lock.lock();

        //如果给锁自定义时间这时就要注意了，锁到期以后不会自动续期
        //所以自动解锁时间一定要大于业务时间
        //lock.lock(10, TimeUnit.SECONDS);
        try{
            System.out.println("加锁成功，执行任务...."+Thread.currentThread().getId());
            Thread.sleep(30000); //模拟业务执行时间：30秒
        }catch (Exception e){

        }
        finally {
            //调用解锁
            lock.unlock();
            System.out.println("释放锁...."+Thread.currentThread().getId());
        }
        return  "hello";
    }

    //测试 读写锁锁  ->写
    @ResponseBody
    @GetMapping("/write")
    public String writeValue(){
        String uuid = "";
        RReadWriteLock lock = redissonClient.getReadWriteLock("rw-lock");
        RLock rLock = lock.writeLock();
        try {
            //1.改写数据加写锁，读数据加读锁
            rLock.lock();
            uuid = UUID.randomUUID().toString();
            Thread.sleep(30000);
            redisTemplate.opsForValue().set("mywrite",uuid);
        }catch (Exception e){

        }finally {
            rLock.unlock();
        }
        return uuid;
    }
    //测试 读写锁锁  ->读
    @ResponseBody
    @GetMapping("/read")
    public String readValue(){
        String uuid = "";
        RReadWriteLock lock = redissonClient.getReadWriteLock("rw-lock");
        RLock rLock = lock.readLock();
        try {
            rLock.lock();
            uuid = redisTemplate.opsForValue().get("mywrite");
        }catch (Exception e){

        }finally {
            rLock.unlock();
        }
        return uuid;
    }


    /**
     *  放假 -》锁门
     *  1班没人  2班没人 3班没人
     *  3个班全部走完才能锁
     */
    //闭锁测试
    @ResponseBody
    @GetMapping("/lockDoor")
    public String lockDoor() throws InterruptedException {
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.trySetCount(3);
        door.await(); //等待闭锁都完成
        return "放假了";
    }

    @ResponseBody
    @GetMapping("/gogogo/{id}")
    public String gogogo(@PathVariable("id") Long id){
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.countDown();  //计数 -1
        return id+"班的人走了";
    }

}
